17 / 19

Why is accessing an array element O(1)?

Constant-Time Array Access

javascript
  1. 1

    Accessing A[0], A[100], or A[1,000,000] requires the same number of address calculations.

  2. 2

    There is no dependency on the number of preceding elements.

  3. 3

    Therefore, indexed read and update operations are O(1).

  4. 4

    This assumes ordinary array access rather than operations that first require searching for an index.

Difficulty: 2/10

Follow-up Questions

  • Why is searching an unsorted array O(n)?
  • Why is insertion in the middle O(n)?